package com.servlet.maintenance.student;

import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import com.dao.impl.*;

public class StudentOpServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor of the object.
	 */
	public StudentOpServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		HttpSession mySession = request.getSession();
		int classId = 0;
		int majorId = 0;
		//int success = 0;
		String className = "";
		try{
			classId = Integer.parseInt((String)mySession.getAttribute("class_id"));
			className = (String)mySession.getAttribute("class_name");
			majorId = Integer.parseInt((String)mySession.getAttribute("major_id"));
			
			String op = request.getParameter("op");
			Map<?,?> course = CourseOP.queryCourse(majorId);
			Collection<?> col = course.keySet();
			Iterator<?> iter = col.iterator();
			if(op.equals("add")){
				String studentName = request.getParameter("name");
				String studentNo = request.getParameter("no");
				//int row = StudentOP.addStudent(studentNo, studentName, classId);
				StudentOP.addStudent(studentNo, studentName, classId);
				//System.out.println("----------"+row);
				while(iter.hasNext()){
					Integer courseId = (Integer)iter.next();
					int couId =  courseId.intValue();
					int score = Integer.parseInt(request.getParameter(courseId.toString()));
					StudentOP.insertStudentScore(studentNo, studentName, classId, couId, score);
				}
			}else if(op.equals("update")){
				String sno = request.getParameter("id");
				while(iter.hasNext()){
					Integer courseId = (Integer)iter.next();
					int couId =  courseId.intValue();
					int score = Integer.parseInt(request.getParameter(courseId.toString()));
					//System.out.println(couId+", "+score);
					StudentOP.updateStudentScore(sno, couId, score);
				}
			}else if(op.equals("del")){
				String sno = request.getParameter("id");
				StudentOP.deleteStudent(sno);
			}else if(op.equals("addcourse")){
				String courseName = request.getParameter("course");
				StudentOP.addCourse(courseName, majorId);
			}else if(op.equals("delcourse")){
				int courseId = Integer.parseInt(request.getParameter("course"));
				//int row = CourseOP.deleteCourse(courseId);
				CourseOP.deleteCourse(courseId);
			}
		}catch(NumberFormatException nfe){
			
		}
		String url = "manager/student/view_student.jsp?id="+classId+"&name="+className;
		url=new String(url.getBytes("GBK"),"ISO8859_1");
		response.sendRedirect(url); 
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request,response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
